#include <gtest/gtest.h>
#include "../utils/test_utils.h"

using namespace TestUtils;

class FeatureConfigTest : public ::testing::Test {
protected:
    void SetUp() override {
        cleanupBuildOutputs();
        // 清理环境变量
        unsetEnvironmentVariable("PROJECT_FEATURE_CONFIG");
    }
    
    void TearDown() override {
        unsetEnvironmentVariable("PROJECT_FEATURE_CONFIG");
        cleanupBuildOutputs();
    }
};

TEST_F(FeatureConfigTest, FeatureConfigurationLoading) {
    TestScope scope("FeatureConfigurationLoading");
    
    // 设置特性配置文件环境变量
    setEnvironmentVariable("PROJECT_FEATURE_CONFIG", "examples/feature_config.txt");
    
    // 构建特性测试目标
    std::string command = Commands::BUILD_TARGET + " " + Targets::FEATURE_TEST;
    auto result = executeBuildCommand(command);
    
    // 验证构建成功
    EXPECT_EQ(result.exitCode, 0) << "Feature config build failed: " << result.output;
    EXPECT_TRUE(result.containsOutput("Build completed successfully")) 
        << "Build success message not found";
    
    // 验证特性配置加载成功
    EXPECT_TRUE(result.containsOutput("Feature config loaded successfully")) 
        << "Feature config loading message not found: " << result.output;
}

TEST_F(FeatureConfigTest, FeatureConfigFileExists) {
    TestScope scope("FeatureConfigFileExists");
    
    // 验证特性配置文件存在
    std::string configPath = "examples/feature_config.txt";
    EXPECT_TRUE(fileExists(configPath)) 
        << "Feature config file not found: " << configPath;
    
    // 读取并验证配置文件内容
    std::string content = readFile(configPath);
    EXPECT_FALSE(content.empty()) << "Feature config file is empty";
}

TEST_F(FeatureConfigTest, WithoutFeatureConfig) {
    TestScope scope("WithoutFeatureConfig");
    
    // 不设置特性配置，构建应该仍然成功
    std::string command = Commands::BUILD_TARGET + " " + Targets::FEATURE_TEST;
    auto result = executeBuildCommand(command);
    
    // 构建应该成功，但可能没有特性配置加载信息
    EXPECT_EQ(result.exitCode, 0) << "Build without feature config failed: " << result.output;
}

TEST_F(FeatureConfigTest, InvalidFeatureConfigPath) {
    TestScope scope("InvalidFeatureConfigPath");
    
    // 设置无效的特性配置文件路径
    setEnvironmentVariable("PROJECT_FEATURE_CONFIG", "non_existent_config.txt");
    
    std::string command = Commands::BUILD_TARGET + " " + Targets::FEATURE_TEST;
    auto result = executeBuildCommand(command);
    
    // 构建可能失败或成功但有警告信息
    // 这里的具体行为取决于构建系统的实现
    if (result.exitCode != 0) {
        // 如果失败，应该有合理的错误信息
        EXPECT_FALSE(result.output.empty()) << "No error message for invalid config";
    }
}

TEST_F(FeatureConfigTest, FeatureConfigEnvironmentIsolation) {
    TestScope scope("FeatureConfigEnvironmentIsolation");
    
    // 测试1：设置特性配置
    setEnvironmentVariable("PROJECT_FEATURE_CONFIG", "examples/feature_config.txt");
    
    std::string command1 = Commands::BUILD_TARGET + " " + Targets::FEATURE_TEST;
    auto result1 = executeBuildCommand(command1);
    
    EXPECT_EQ(result1.exitCode, 0) << "First build failed";
    
    // 测试2：清理环境变量后重新构建
    unsetEnvironmentVariable("PROJECT_FEATURE_CONFIG");
    
    auto result2 = executeBuildCommand(command1);
    
    EXPECT_EQ(result2.exitCode, 0) << "Second build failed";
    
    // 两次构建的输出应该不同（一个有特性配置，一个没有）
    bool firstHasFeatureConfig = result1.containsOutput("Feature config loaded successfully");
    bool secondHasFeatureConfig = result2.containsOutput("Feature config loaded successfully");
    
    if (firstHasFeatureConfig) {
        EXPECT_FALSE(secondHasFeatureConfig) 
            << "Feature config should not be loaded in second build";
    }
}

TEST_F(FeatureConfigTest, FeatureConfigContentValidation) {
    TestScope scope("FeatureConfigContentValidation");
    
    // 读取特性配置文件并验证内容格式
    std::string configPath = "examples/feature_config.txt";
    ASSERT_TRUE(fileExists(configPath)) << "Feature config file not found";
    
    std::string content = readFile(configPath);
    ASSERT_FALSE(content.empty()) << "Feature config file is empty";
    
    // 验证配置文件包含预期的配置项
    // 这里的具体验证取决于feature_config.txt的实际内容格式
    EXPECT_TRUE(containsString(content, "=") || containsString(content, ":")) 
        << "Config file should contain key-value pairs";
}

TEST_F(FeatureConfigTest, FeatureConfigBuildIntegration) {
    TestScope scope("FeatureConfigBuildIntegration");
    
    setEnvironmentVariable("PROJECT_FEATURE_CONFIG", "examples/feature_config.txt");
    
    // 构建整个项目，验证特性配置不影响其他目标
    auto result = executeBuildCommand(Commands::BUILD_ALL);
    
    EXPECT_EQ(result.exitCode, 0) << "Full build with feature config failed: " << result.output;
    EXPECT_TRUE(result.containsOutput("Build completed successfully")) 
        << "Build success message not found";
    
    // 验证其他目标仍然正常构建
    std::string helloWorldPath = Paths::OUT_RELEASE + "/" + Targets::HELLO_WORLD;
    EXPECT_TRUE(fileExists(helloWorldPath)) 
        << "Other targets should still build correctly";
}